package cn.itcast.netty.c1;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;

import java.net.InetSocketAddress;

/**
 * @Author: LDeng
 * @Date: 2021-05-17 14:08
 */
public class HelloClient {
    public static void main(String[] args) throws InterruptedException {
        //1, 启动类
        new Bootstrap()
                //2，添加EventLoop
                .group(new NioEventLoopGroup())
                //3, 选择客户端channel实现
                .channel(NioSocketChannel.class)
                .handler(
                        new ChannelInitializer<NioSocketChannel>() {
                            @Override//在连接建立后才会被调用
                            protected void initChannel(NioSocketChannel ch) throws Exception {
                                ch.pipeline().addLast(new StringEncoder());
                            }
                        })
                //5 ,连接到服务器
                .connect(new InetSocketAddress("localhost", 8080))
                .sync()//客户端这边是一个阻塞方法，知道连接建立
                .channel()//代表连接对象
                //6, 向服务器发送数据
                .writeAndFlush("hello,worldworldworldworldworld");//发送数据


    }
}
